package 其他;
import java.util.*;


public class Solution {
        /**
         * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
         * 超市打折，买一送一，每两件商品中价格取大的那一件，给定一字符串数组，字符串代表商品价格，
         * 问买完数组里的商品，至少需要多少钱
         *
         * @param skuAmount string字符串一维数组
         * @return string字符串
         */
        public static String calcPayAmount (String[] skuAmount) {
            // write code here
            int length = skuAmount.length;
            Float[] arr = new Float[length];
            //转成float数组
            for (int i = 0; i < length; i++) {
                arr[i] = Float.valueOf(skuAmount[i]);
            }
            //排序
            Sort(arr, 0, length - 1);
            Float amount = new Float(0);
            for(int i = length - 1; i >= 0 ; ){
                    amount += arr[i];
                    i = i - 2;
            }
            return amount.toString();
        }

        public static void Sort(Float[] arr, int start, int end){
            if (start < end) {
                int i = start, j = end;
                while (i < j) {
                    while (i < j && arr[i] <= arr[j]) j--;
                    if (i < j) {
                        float t1 = arr[i];
                        arr[i] = arr[j];
                        arr[j] = t1;
                        i++;
                    }
                    while (i < j && arr[i] <= arr[j]) i++;
                    if (i < j) {
                        float t1 = arr[i];
                        arr[i] = arr[j];
                        arr[j] = t1;
                        j--;
                    }
                }
                Sort(arr, start, i - 1);
                Sort(arr, i + 1, end);
            }
        }

        public static void main(String[] args) {
            String[] a = {"199.90","39.90","29.90","59.90", "10"};
            String s = calcPayAmount(a);
            System.out.println(s);
        }

}
